package com.lewin.ringroad.net.message;

import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域统计数据
 *
 * @author Jun
 * @since 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ZoneStatisticMessage extends BaseMessage {
    //@formatter:off
    /** 检车通道数量 */
    private short checkChannelCount;

    /** 报文接收时间 */
    private long receivedTime = System.currentTimeMillis();

    /** 检测通道数据 */
    private List<ChannelStatisticsData> channelDataList;

    //@formatter:on

    @Override
    public BaseMessage parse(ByteBuf buf) {
        this.checkChannelCount = buf.readUnsignedByte();
        this.channelDataList = new ArrayList<>(this.checkChannelCount);
        for(int i = 0; i < this.checkChannelCount; i++) {
            this.channelDataList.add(ChannelStatisticsData.parseFrom(buf));
        }

        return this;
    }

    /**
     * 通道统计数据
     */
    @Data
    public static class ChannelStatisticsData {
        //@formatter:off

        /** 检测通道号 */
        private short code;
        /** 通道车辆数 */
        private short vehicleCount;
        /** 通道空间利用率 */
        private short spaceUseRate;
        /** 平均车速 */
        private short averageSpeed;
        /** 分布情况 */
        private short distribution;
        /** 头车位置 */
        private short headVehicleLocation;
        /** 透彻速度 */
        private short headVehicleSpeed;
        /** 末车位置 */
        private short tailVehicleLocation;
        /** 末车速度 */
        private short tailVehicleSpeed;

        //@formatter:on

        public static ChannelStatisticsData parseFrom(ByteBuf buf) {
            ChannelStatisticsData data = new ChannelStatisticsData();
            data.code = buf.readUnsignedByte();
            data.vehicleCount = buf.readUnsignedByte();
            data.spaceUseRate = buf.readUnsignedByte();
            data.averageSpeed = buf.readUnsignedByte();
            data.distribution = buf.readUnsignedByte();
            data.headVehicleLocation = buf.readUnsignedByte();
            data.headVehicleSpeed = buf.readUnsignedByte();
            data.tailVehicleLocation = buf.readUnsignedByte();
            data.tailVehicleSpeed = buf.readUnsignedByte();

            return data;
        }
    }
}
